
#include "base_malloclib.h"

// WSL禁用
#ifndef WSLINUX
	#define WFSPAN_MADVISE_FREE	(MADV_DONTNEED)		/* 使用MADV_DONTNEED释放内存 */
#endif

#define WFSPAN_MBLOCK		(SIZE_2M)		/* 归还给操作系统的MBLOCK基本单位 */

/* 引入mmapheap */
#include <metalc/mlos/mmapheap.h>
/* 可以修改大小的*/
DEF_MMAPHEAP_MLAYER(mmapheap_normal, WFSPAN_MBLOCK, SIZE_1G, SIZE_64G, 0, 0)

/* 定义wfspan */
#include <metalc/mlayer/wfspan.h>

/* 使用转换表加速 */
DEF_CLS_SUPER_CLS2SZTBL(super, 4, 15, MANTISSA_BITS_DEFAULT, LOW_BITS_DEFAULT)

#define CLS2SZ_SUPER(cls)	__cls2sz_super(cls, MANTISSA_BITS_DEFAULT, LOW_BITS_DEFAULT)

// static const size_t __cls2sz_tbl_a [] = { MAPC(CLS2SZ_SUPER, RANGE8(64)) };\

#define WFSPAN_SLABSIZE		65536
#define WFSPAN_CLS_UNITNR(cls)	(WFSPAN_SLABSIZE/__cls2sz_super(cls, MANTISSA_BITS_DEFAULT, LOW_BITS_DEFAULT))
#define WFSLAB_CLS_UNITNR(cls)	((WFSPAN_SLABSIZE - sizeof(mslab_t))/__cls2sz_super(cls, MANTISSA_BITS_DEFAULT, LOW_BITS_DEFAULT))
#define WFSLAB_SLABLIMIT_K	64	/* 线程拥有的slab数量限制 */
#ifndef WFSLAB_FRAC_P
	#define WFSLAB_FRAC_P	0	/* 查询线程个数为当前总线程数除以frac_p */
#endif
#define WFSLAB_HELPQUOTA_H	1	/* 帮助限额，除了wfqueue protocol帮助协议外，每次帮助推进1次即可 */


SIN hword_t unitsz2maxnr(szcls_t cls){
	/* 提前算好的, 39个map映射表 */
	/* FIXME: 启用WFSPAN_MBLOCK的话这个需要改变，还需要加一个szcls */
#ifdef WFSPAN_MBLOCK
	static const uint16_t maptable[] = {
		MAPC(WFSPAN_CLS_UNITNR, RANGE8(40))};

#else
	static const uint16_t maptable[] = {
		MAPC(WFSLAB_CLS_UNITNR, RANGE8(39))};
#endif
	ASSERT(cls <= _clsnr_super);
	return (hword_t)maptable[cls];
}

DEF_WFSPAN_MLAYER(wfspan, 4, 15, WFSPAN_SLABSIZE, WFSLAB_SLABLIMIT_K, WFSLAB_FRAC_P, WFSLAB_HELPQUOTA_H, super, unitsz2maxnr, mlcc_palloc, HDEF_PTR_BITS, page_alloc)
// DEF_WFSLAB_MLAYER(wfspan, 4, 15, BIT(16), 64, 0, 1, super, unitsz2maxnr, mlcc_palloc)

/* 组合 */
// COMPOSE_MLAYER(malloc_wfslab, mallocapi, wfspan, seglf_bin_ext, mmapheap_normal)
COMPOSE_MLAYER(malloc_wfslab, mallocapi, wfspan, mmapheap_normal)

/* 定义tls */
DEF_MTLS_PTHREAD(GET_MLAYER_OBJNR(malloc_wfslab), 4096, mlcc_palloc, mp_coreid)

/* mlcc和mtls一定要放在第一位初始化 */
DEF_SYSINIT(malloc_perm_pool, malloc_wfslab, mtls)
DEF_CFLOWINIT(mtls, malloc_wfslab)
DEF_CFLOWFIN(mtls)
DEF_SYSFIN(__omega)